package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.activity.ChoiceActivity;
import com.jdy.haoduoaiteacher.model.CategoryItem;
import com.jdy.haoduoaiteacher.util.CommonUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * 
 * @author lzq
 * 
 */
@SuppressLint("ClickableViewAccessibility")
public class CategoryGridViewAdapter extends BaseAdapter {
  private final Context mContext;
  private ArrayList<CategoryItem> mList;
  private DisplayImageOptions options;
  private OnGridItemClickListener mGridItemClickListener;

  public interface OnGridItemClickListener {
    public void onGridItemClik(int pos);
  }

  public OnGridItemClickListener getDateGridItemClickListener() {
    return mGridItemClickListener;
  }

  public void setOnGridItemClickListener(
      OnGridItemClickListener mGridItemClickListener) {
    this.mGridItemClickListener = mGridItemClickListener;
  }

  public CategoryGridViewAdapter(Context context, ArrayList<CategoryItem> list) {
    this.mContext = context;
    this.mList = list;
    // 用 displayImage 加载图片 的 配置参数
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    // 创建默认的ImageLoader配置参数
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs() // 打印 log 信息
        .build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    CategoryItem item = null;
    if (mList != null && !mList.isEmpty()) {
      item = mList.get(position);
    }

    ViewHolder holder;

    if (convertView == null) {
      convertView = LayoutInflater.from(mContext).inflate(
          R.layout.item_grid_text_teacher, parent, false);
      holder = new ViewHolder();
      holder.title = (TextView) convertView.findViewById(R.id.title);
      holder.imgCover = (ImageView) convertView.findViewById(R.id.cover);

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (item != null) {
      ImageLoader.getInstance().displayImage(item.icon, holder.imgCover,
          options);
      holder.imgCover.setTag(item);
      holder.title.setText(item.name);
    }

    addTouchDrak(holder.imgCover);
    return convertView;
  }

  private static class ViewHolder {
    private TextView title;
    private ImageView imgCover;
  }

  /**
   * 给试图添加点击效果,让背景变深
   * */
  public void addTouchDrak(View v) {
    v.setOnTouchListener(CommonUtil.VIEW_TOUCH_DARK);
    v.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        CategoryItem categoryItem = (CategoryItem) arg0.getTag();
        if (categoryItem.name.equals("经典绘本")) {
          // Intent intent = new Intent();
          // Bundle bundle = new Bundle();
          // intent.setClass(mContext, IllustratedStoryActivity.class);
          // bundle.putSerializable("categoryitem", categoryItem);
          // intent.putExtras(bundle);
          // mContext.startActivity(intent);
        } else {
          Intent intent = new Intent();
          Bundle bundle = new Bundle();
          intent.setClass(mContext, ChoiceActivity.class);
          bundle.putSerializable("categoryitem", categoryItem);
          intent.putExtras(bundle);
          mContext.startActivity(intent);
        }
      }
    });
  }

}
